package com.example.common.dto.base;

import com.example.common.constant.HttpStatus;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
/**
 * 统一消息返回模板
 *
 * @author huangjie
 * @create 2025/8/27
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultMsg<T> {
    @ApiModelProperty(value  = "状态码")
    private Integer code;
    @ApiModelProperty(value  = "消息")
    private String msg;
    @ApiModelProperty(value  = "数据列表")
    private T data;
    @ApiModelProperty(value  = "错误详情")
    private String error;

    public ResultMsg() {
    }

    public ResultMsg(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultMsg(Integer code, String msg, T data, String error) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.error = error;
    }

    public static <T> ResultMsg<T> generateSuccessResults() {
        return new ResultMsg<T>(HttpStatus.OK.value(), "操作成功", null);
    }

    public static <T> ResultMsg<T> generateResults(Integer code, String msg, T data) {
        return new ResultMsg<T>(code, msg, data);
    }

    public static <T> ResultMsg<T> generateResults(Integer code, String msg, T data, String error) {
        return new ResultMsg<T>(code, msg, data, error);
    }

    public static <T> ResultMsg<T> generateSuccessResults(T data) {
        return new ResultMsg<T>(HttpStatus.OK.value(), "操作成功", data);
    }

    public static <T> ResultMsg<T> generateSuccessResults(String msg) {
        return new ResultMsg<T>(HttpStatus.OK.value(), msg, null);
    }

    public static <T> ResultMsg<T> generateSuccessResults(String msg, T data) {
        return new ResultMsg<T>(HttpStatus.OK.value(), msg, data);
    }

    public static <T> ResultMsg<T> generateFailResults500(String errorMsg) {
        return new ResultMsg<T>(HttpStatus.INTERNAL_SERVER_ERROR.value(), errorMsg, null);
    }

    public static <T> ResultMsg<T> generateFailResults400(String errorMsg) {
        return new ResultMsg<T>(HttpStatus.BAD_REQUEST.value(), errorMsg, null);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }
}

